package edu.boun.g4.coursity.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import edu.boun.g4.coursity.domain.SemanticTag;
import edu.boun.g4.coursity.freebase.FreebaseSemanticTagging;
import edu.boun.g4.coursity.freebase.FreebaseTag;
import edu.boun.g4.coursity.service.SemanticTagService;
import edu.boun.g4.coursity.service.UserService;

/**
 * @author mertusta
 * 
 *         Handles and retrieves semanticTag request
 */
@Controller
@RequestMapping("/semanticTag")
public class SemanticTagController {

	@Resource(name = "semanticTagService")
	private SemanticTagService semanticTagService;

	@Resource(name = "userService")
	private UserService userService;
	
	

	@RequestMapping(method = RequestMethod.POST, value = "/getTags")
	public @ResponseBody
	Response getTags(@RequestParam String text) {

		Response response = new Response();
		response.setBody(null);
		response.setStatus(true);

		FreebaseSemanticTagging tagging = new FreebaseSemanticTagging();
		List<FreebaseTag> list = tagging.getTags(text);
		response.setBody(list);

		return response;
	}

	@RequestMapping(method = RequestMethod.POST, value = "/add")
	public @ResponseBody
	Response addSemanticTag(@RequestParam String name,
			@RequestParam String notableId, @RequestParam String notableName,
			@RequestParam Integer userId, @RequestParam Integer courseId) {

		SemanticTag semanticTag = new SemanticTag();
		semanticTag.setTagName(name);
		semanticTag.setTagNotableId(notableId);
		semanticTag.setTagNotableName(notableName);
		semanticTag.setTagger(userService.get(userId));

		Response response = new Response();
		response.setBody(semanticTag);
		response.setStatus(true);

		semanticTagService.add(courseId, semanticTag);

		return response;
	}

	@RequestMapping(method = RequestMethod.POST, value = "/delete")
	public @ResponseBody
	Response deleteSemanticTag(@RequestParam Integer id) {

		Response response = new Response();
		response.setBody(null);
		response.setStatus(true);

		semanticTagService.delete(id);

		return response;
	}

	@RequestMapping(method = RequestMethod.POST, value = "/edit")
	public @ResponseBody
	Response editSemanticTag(@RequestParam String name, @RequestParam String uri) {

		SemanticTag existingSemanticTag = new SemanticTag();

		Response response = new Response();
		response.setBody(null);
		response.setStatus(true);

		semanticTagService.edit(existingSemanticTag);

		return response;
	}

	@RequestMapping(value = "/list")
	public @ResponseBody
	Response listUser() {

		List<SemanticTag> all = semanticTagService.getAll();

		Response response = new Response();
		response.setBody(all);
		response.setStatus(true);

		return response;
	}

}